package com.blank.white.common.exception;


import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/**
 * @desc 自定义异常基础类，用户异常
 * @classname BaseException
 */
public class AuthException extends RuntimeException implements Serializable {

    private static final long serialVersionUID = 226438603019156261L;

    private final ErrorsEnum error;

    private final HashMap<String, Object> data = new HashMap<>();

    public AuthException(ErrorsEnum error) {
        this.error = error;
    }


    public AuthException(ErrorsEnum error, Map<String, Object> data) {
        super(error.getMessage());
        this.error = error;
        if (!ObjectUtils.isEmpty(data)) {
            this.data.putAll(data);
        }
    }

    public AuthException(ErrorsEnum error, Throwable throwable) {
        super(error.getMessage(), throwable);
        this.error = error;
    }

    public AuthException addAttribute(String key, Object val) {
        data.put(key, val);
        return this;
    }

    protected AuthException(ErrorsEnum error, Map<String, Object> data, Throwable cause) {
        super(error.getMessage(), cause);
        this.error = error;
        if (!ObjectUtils.isEmpty(data)) {
            this.data.putAll(data);
        }
    }

    public ErrorsEnum getError() {
        return error;
    }

    public Map<String, Object> getData() {
        return data;
    }

    @Override
    public String toString() {
        String message = super.toString() +
                ", error=" + error +
                ", data=" + data;
        return message;
    }
}
